package com.yx.learn.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.yx.learn.dao.AuthorityDao;
import com.yx.learn.domain.Authority;

@Repository("authorityDao")
public class AuthorityDaoImpl implements AuthorityDao {
	
	@Autowired
	@Qualifier("hibernateTemplate")
	private HibernateTemplate hibernateTemplate;

	@SuppressWarnings("unchecked")
	public List<Authority> getAuthorities() throws Exception {
		// TODO Auto-generated method stub
		return this.hibernateTemplate.find("From Authority");
	}

	public List<Authority> getAuthoritiesByPid(int pid) throws Exception {
		// TODO Auto-generated method stub
		String hql = null;
		if(pid==0){
			hql="From Authority where pid=-1";
		}else{
			hql="From Authority where pid='"+pid+"'";
		}
		@SuppressWarnings("unchecked")
		List<Authority> authorities = hibernateTemplate.find(hql);
		return authorities;

	}
	
	public List<Authority> getAuthoritiesByCondition(String hql)
			throws Exception {
		// TODO Auto-generated method stub
		@SuppressWarnings("unchecked")
		List<Authority> authorities = hibernateTemplate.find(hql);
		return authorities;
	}

	public Authority findById(int id) throws Exception {
		// TODO Auto-generated method stub
		String hql="From Authority where id=?";
		@SuppressWarnings("unchecked")
		List<Authority> list = hibernateTemplate.find(hql,id);
		return list.isEmpty() ? null : list.get(0);

	}

	public void save(Authority authority) throws Exception {
		// TODO Auto-generated method stub
		this.hibernateTemplate.save(authority);
	}

	public void update(Authority authority) throws Exception {
		// TODO Auto-generated method stub
		this.hibernateTemplate.merge(authority);
		
	}

	public void delete(Authority authority) throws Exception {
		// TODO Auto-generated method stub
		this.hibernateTemplate.delete(authority);
		
	}

}
